package com.secneo.appshare.common;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;
import java.util.List;
import com.secneo.appshare.database.AppFile;
import com.secneo.appshare.database.AppShareDatabaseHelper;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;

public class ReadLogCat {
	public static BufferedReader reader = null;
	public static Object localObject = "/";
	public static String logString = "";
	private static String packageName = "";

	public static void getLog(Context context) {

		try {
			
			Process proc = Runtime.getRuntime().exec("logcat -s ActivityManager"); //$NON-NLS-1$
			localObject = context;
			localObject = new InputStreamReader(proc.getInputStream());
			reader = new BufferedReader((Reader) localObject, 8192 * 4);
			
			do {
				do {
					logString = new String();
					logString = reader.readLine();
				} while (logString == null||"".equals(logString));
				// "Starting activity:"为启动程序标示
				if (logString.contains("android.intent.category.LAUNCHER")) {
					getLogs(context);
				}
				
			} while (logString != null);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void getLogs(Context context) {

		try {
			
				boolean localboolean = false;
				localboolean = logString.contains("comp=");
				if (localboolean) {
					packageName = logString.split("comp=\\{")[1].split("/")[0];
					localObject = context;

				} else {

					packageName = logString.split("cmp=")[1].split("/")[0];
					localObject = context;
				}
			
			final PackageManager pm = context.getPackageManager();
			List<ApplicationInfo> userApps = Util.filterUserApps(pm.getInstalledApplications(0));
			for (int i = 0; i < userApps.size(); i++)
			{
				if (userApps.get(i).packageName.equals(packageName))
				{
					
					AppShareDatabaseHelper dh = new AppShareDatabaseHelper(context);
					
						AppFile appInfo = dh.getAppFileByPackageName(dh, packageName);
						if(appInfo == null)
						{
							  String appName=null;//程序名称
							  PackageManager manager = context.getPackageManager();
							  try { 
							  PackageInfo info = manager.getPackageInfo(packageName, 0);
							  appName=info.applicationInfo.loadLabel(context.getPackageManager()).toString();
							  
							  } catch (NameNotFoundException e) {
								  e.printStackTrace();
							  }

							AppFile appFile=new AppFile();
							appFile.setPackageName(packageName);
							appFile.setName(appName);
							appFile.setRunCount(1);
							dh.insertAppFile(appFile);
						}
						else
						{
							int useNum_str = appInfo.getRunCount();
							useNum_str++;
							appInfo.setRunCount(useNum_str);
							dh.updateAppFile(appInfo);
						}
						dh.close();
					}
					
				}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}